<?php


namespace app\admin\controller;
use  think\Controller;
class Article extends Controller{
	//添加文章
  	public function add(){

  		if(request()->isAjax()){
  			$data=[
  				'title'=>input('post.title'),
  				'tags'=>input('post.tag'),
  				'cate_id'=>input('post.cate_id'),
  				'desc'=>input('post.desc'),
  				'content'=>input('post.content')
  			];
  			$result=model('Article')->add($data);
  			if($result==1){
  				$this->success('添加成功','admin/article/list_l');
  			}else{
  				$this->error($result);
  			}
  		}
      $cates=model('Cate')->select();
      $viewData=[
      'cates'=>$cates
      ];
      $this->assign($viewData);
  		return view();
  	}

      //文章列表
  	  public function list_l(){
        $articles=model('Article')->with('cate')->order('id','desc')->paginate(10);
        $viewData=[
            'articles'=>$articles
        ];
        $this->assign($viewData);
  		return view();
  	}
    //推荐操作
    public function top(){
      if(request()->isAjax()){
        $data=[
              'id'=>input('post.id'),
              'is_top'=>input('post.is_top')? 0:1
        ];
        $result=model('Article')->top($data);
        if($result==1){
            $this->success('操作成功！','admin/article/list_l');

        }else{
          $this->error($result);
        }

      }
    }
    //删除文章
    public function del(){
      if(request()->isAjax()){
      $articleInfo=model('Article')->with('content')->find(input('post.id'));
      $result=$articleInfo->together('content')->delete();
    if($result==1){
      $this->success('删除成功！');//,'admin/cate/list'
    }else{
      $this->error($result);
    }
      }
    }

    //修改文章
    public function edit(){
      if(request()->isAjax()){
        $data=[
          'title'=>input('post.title'),
          'id'=>input('post.id'),
          'tags'=>input('post.tags'),
          'cate_id'=>input('post.cate_id'),
          'desc'=>input('post.desc'),
          'content'=>input('post.content')
        ];
        $result=model('Article')->edit($data);
        if($result==1){
        $this->success('修改成功！','admin/article/list_l');//,'admin/cate/list'
        }else{
        $this->error($result);
        }


      }
      $articleInfo=model('Article')->find(input('id'));
      $cates=model('Cate')->select();
      $viewData=[
          'articleInfo'=>$articleInfo,
          'cates'=>$cates
      ];
      $this->assign($viewData);
      return view();
    }

}